/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月25日
 */
package com.massyframework.beanskin.assembly.beans;

import java.util.List;

/**
 * Bean注册器, 用于向BeanContext注入Bean对象实例。
 * @author huangkh
 *
 */
public interface BeanRegistry {

	/**
	 * 添加Bean实例
	 * @param beanName {@link String},bean名称
	 * @param bean {@link Object},bean实例
	 * @throws BeanExistsException 如果存在同名的bean,则抛出例外
	 */
	void addBean(String beanName, Object bean) throws BeanExistsException;
	
	/**
	 * 判断名称为{@code name}的bean是否存在
	 * @param name {@link String},bean名称
	 * @return {@link boolean},返回{@code true}表示存在，否则返回{@code false}.
	 */
	boolean containsBean(String name);
	
	
	/**
	 * 根据{@code beanName}获取对应的Bean
	 * @param beanName {@link String},bean名称
	 * @return {@link Object},无对应bean可返回null.
	 */
	Object getBean(String beanName);
	
	
	/**
	 * 根据{@code name}获取对应的Bean,并按{@code requiredType}类型返回
	 * @param <T>
	 * @param name {@link String},bean名称
	 * @param requiredType {@link Class},要返回的类型
	 * @return {@link T}, bean不存在，或者不支持{@code requiredType},则返回null.
	 */
	default <T> T getBean(String name, Class<T> requiredType) {
		Object result = this.getBean(name);
		return result == null ? null :
			requiredType.isAssignableFrom(result.getClass()) ?
					requiredType.cast(result) : null;
	}
	
	/**
	 * 所有Bean名称
	 * @return {@link List}
	 */
	List<String> getBeanNames();
}
